package com.cms.meeting.controller;

import com.cms.meeting.entity.Department;
import com.cms.meeting.entity.Employee;
import com.cms.meeting.service.DepartmentService;
import com.cms.meeting.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    DepartmentService departmentService;

    @RequestMapping("/")
    public String Login(){
        return "login";
    }
    @RequestMapping("doLogin")
    public String doLogin(String username, String password, Model model, HttpSession httpSession){
        Employee employee =employeeService.doLogin(username,password);
        if (employee==null){
            model.addAttribute("error","用户名不存在或者密码错误");
            return "forward:/";
        }
        else{
            if(employee.getStatus()==0){
                model.addAttribute("error","用户待审批");
                return "forward:/";
            }
             else if(employee.getStatus()==2){
                model.addAttribute("error","用户审批未通过");
                return "forward:/";
            }
             else {
                 httpSession.setAttribute("currentUser",employee);
                 return "redirect:/notifications";
            }
        }
    }
    @RequestMapping("/register")
    public String RegisterController(Model model){
        List<Department> deps =  departmentService.getAllDeps();
        model.addAttribute("deps",deps);
        return "register";
    }
    @RequestMapping("/doReg")
    public String doReg(Employee employee,Model model){
        Integer result = employeeService.doReg(employee);
    if (result==1){
        return "redirect:/";
    }else {
        model.addAttribute("error", "注册失败");
        model.addAttribute("employee", employee);
        return "forward:/register";
    }
    }
}
